package Comunication;

public class Use {
	Message message = null;
	boolean empty = true;

	public synchronized Message extract(int id) {
		if (message == null || message.flags[id] == true) {
			try {
				System.out.println("wait");
				wait();
				System.out.println("fine wait");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("estratto");
		}
		message.flags[id] = true;
		return message;
	}

	public void insert(Message m) {
		System.out.println("inserisco");
		if (message != null) {
			for (boolean b : message.flags) {
				if (b != true) {
					System.out.println("Bus Pieno");
					try {
						System.out.println("DORMO");
						Thread.sleep(10000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
		message = m;
		notifyAll();
		System.out.println("inserito");
	}
}
